package eleanor.ltd.isne.security;

import java.io.IOException;

import org.apache.tapestry5.Link;
import org.apache.tapestry5.runtime.Component;
import org.apache.tapestry5.services.ComponentEventRequestParameters;
import org.apache.tapestry5.services.ComponentRequestFilter;
import org.apache.tapestry5.services.ComponentRequestHandler;
import org.apache.tapestry5.services.ComponentSource;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.apache.tapestry5.services.PageRenderRequestParameters;
import org.apache.tapestry5.services.Response;

import eleanor.ltd.isne.annotations.AdministratorAccess;
import eleanor.ltd.isne.annotations.BuyerAccess;
import eleanor.ltd.isne.pages.Contact;
import eleanor.ltd.isne.pages.Index;
import eleanor.ltd.isne.pages.Login;
import eleanor.ltd.isne.services.Authenticator;

public class AuthenticationFilter implements ComponentRequestFilter {

	private final PageRenderLinkSource renderLinkSource;

    private final ComponentSource componentSource;

    private final Response response;

    private final Authenticator authenticator;

    private String IndexPage = Index.class.getSimpleName();

    private String loginPage = Login.class.getSimpleName();

    public AuthenticationFilter(PageRenderLinkSource renderLinkSource,
                    ComponentSource componentSource, Response response,
                    Authenticator authenticator) {
            this.renderLinkSource = renderLinkSource;
            this.componentSource = componentSource;
            this.response = response;
            this.authenticator = authenticator;
    }

    public void handleComponentEvent(
                    ComponentEventRequestParameters parameters,
                    ComponentRequestHandler handler) throws IOException {

            if (dispatchedToLoginPage(parameters.getActivePageName())) {
                    return;
            }

            handler.handleComponentEvent(parameters);

    }

    public void handlePageRender(PageRenderRequestParameters parameters,
                    ComponentRequestHandler handler) throws IOException {

            if (dispatchedToLoginPage(parameters.getLogicalPageName())) {
                    return;
            }

            handler.handlePageRender(parameters);
    }

    private boolean dispatchedToLoginPage(String pageName) throws IOException {
    		Component pageComponent = componentSource.getPage(pageName);
            Link link = renderLinkSource.createPageRenderLink(Login.class);
            System.out.println("PAGE NAME STRANICE: " + pageName);
            
            if (!loginPage.equalsIgnoreCase(pageName)) {
	            if(authenticator.isLoggedIn()) {
	            	System.out.println("User logovan!");
	            	if(pageComponent.getClass().isAnnotationPresent(AdministratorAccess.class)) {
	            		System.out.println("Anotacija: AdministratorAccess");
	            		
	                	if(authenticator.isAdmin()) {
	                		System.out.println("User je administrator!");
	                		return false;
	                	}
	                }
	            	return false;
	            } 
            } else {
            	return false;
            }
            if(pageComponent.getClass().isAnnotationPresent(BuyerAccess.class)) {
        		
            	return false;
        	}
            System.out.println("Redirect na Login");
//        	Link linkToLogin = renderLinkSource.createPageRenderLink(Login.class);
        	response.sendRedirect(link);
            return true;
            
            
    }

}
